import type { Slide, PPTElement } from '@/types/slides'
import { GetTemplateItemData } from './checkTemplate'
import { nanoid } from 'nanoid'

/**
 * @name 生成单张图片和一段文本的幻灯片
 * @param tempId 模板ID
 * @param params 
 * @returns 
 */
export async function GetTxtAdnImgContCourseware(tempId: string, params: PPTSingleImgSingleTxtDataSchema): Promise<Slide | undefined> {
  const tempData = await GetTemplateItemData(tempId, 'txtAndImg')
  if (!tempData) return

  let str = JSON.stringify(tempData.default)
  str = str.replace('%幻灯片章节%', params.group)
  str = str.replace('%幻灯片段落%', params.txt)
  //   str = str.replace('%幻灯片单图%', params.img)
  const contItem = JSON.parse(str)

  const imgPPTIndex = contItem.elements.findIndex((el: PPTElement) => el.type === 'shape' && el.text?.content.includes('%幻灯片单图%'))

  if (imgPPTIndex !== -1) {
    const item = contItem.elements[imgPPTIndex]
    const imagePPTEl = {
      'type': 'image',
      'id': imgPPTIndex,
      'src': params.img,
      'width': item.width,
      'height': item.height,
      'left': item.left,
      'top': item.top,
      'fixedRatio': true,
      'rotate': 0
    }

    contItem.elements.splice(imgPPTIndex, 1, imagePPTEl)
  }

  contItem.id = nanoid(10)
  contItem.elements.forEach((el: any) => {
    el.id = nanoid(10)
  })
  return contItem
}
